package com.camus.verify.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.camus.verify.entity.VerifyTaskInvoice;
import com.camus.verify.enums.IsMatchedEnum;
import com.camus.verify.mapper.VerifyTaskInvoiceMapper;
import com.camus.verify.service.IVerifyTaskInvoiceService;

/**
 * <p>
 * 核销任务-发票明细表 服务实现类
 * </p>
 *
 * @author jie.deng
 * @since 2021-09-04
 */
@Service
public class VerifyTaskInvoiceServiceImpl extends ServiceImpl<VerifyTaskInvoiceMapper, VerifyTaskInvoice>
		implements IVerifyTaskInvoiceService {

	@Override
	public List<VerifyTaskInvoice> listUnmatchedByTaskCode(String taskCode) {
		LambdaQueryWrapper<VerifyTaskInvoice> queryWrapper = Wrappers.lambdaQuery(VerifyTaskInvoice.class)
				.eq(VerifyTaskInvoice::getTaskCode, taskCode)
				.eq(VerifyTaskInvoice::getMatched, IsMatchedEnum.Not.getCode());
		List<VerifyTaskInvoice> list = getBaseMapper().selectList(queryWrapper);
		return list;
	}

	@Override
	public void updateMatchedBatchByIds(List<Long> ids) {
		LambdaUpdateWrapper<VerifyTaskInvoice> lambdaUpdate = Wrappers.lambdaUpdate(VerifyTaskInvoice.class);
		lambdaUpdate.in(VerifyTaskInvoice::getPkid, ids);
		lambdaUpdate.eq(VerifyTaskInvoice::getMatched, IsMatchedEnum.Not.getCode());

		lambdaUpdate.set(VerifyTaskInvoice::getMatched, IsMatchedEnum.Yes.getCode());
		// lambdaUpdate.set(VerifyTaskInvoice::getLastModifiedTime, LocalDateTime.now());

		int cnt = getBaseMapper().update(null, lambdaUpdate);
		Assert.isTrue(cnt == ids.size(), "回写核销任务发票表异常");
	}

}
